﻿{include file="common/header" /}
<script>
    Handlebars.registerHelper('getAssignStatus', function(v1) {
        if(v1)
            return `<span style="color: red;">已分配</span>`
        else
            return '未分配';
    });
</script>
<div class="page-container">
	<form class="text-c mt-20" method="post" id="myform">
		<input type="text" class="input-text" style="width:250px" placeholder="name" value="" name="name">
		<button type="button" class="btn btn-success" onclick="getData()"><i class="Hui-iconfont"></i> 搜索</button>
	</form>
	<div class="mt-20"></div>
	<style>
		.layui-table{
			display: block !important;
		}
	</style>
	<table id="table"  class="table table-border table-bordered table-bg tablesorter"  >
		<thead>
		<tr class="text-c">
			<td width="25"></td>
			<th width="150">Name</th>
			<th width="90">priority</th>
			<th width="150">depot</th>
			<th>capacity limits</th>
			<th>max stops on route</th>
			<th width="100">isAssign</th>
		</tr>
		</thead>
		<tbody id="tbody">
		</tbody>
	</table>
</div>

<div class="m-style M-box"></div>
<script id="table-lists" type="text/template">
	{{#each data}}
	<tr class="text-c">
		<td><a title="设置" href="javascript:;"
			   onclick="assign('{{vehicle_id}}')" class="ml-5" style="text-decoration:none">
			<i class="Hui-iconfont"></i>

		</a>
		</td>
		<td>{{name}}</td>
		<td class="td-status" >{{priority}}</td>
		<td class="td-status" >{{depot}}</td>
		<td class="td-status"></td>
		<td class="td-status">{{max_stops}}</td>
		<td class="td-manage">{{{getAssignStatus isAssign}}}</td>
	</tr>
	{{/each}}
</script>
<input type="hidden" value="{{$Think.get.id}}" name="driver_id" id="driver_id">
<script type="text/javascript">

    getData();
    //异步获取数据
    function getData(page = 1){
        $.ajax({
            type: 'POST',
            url: '{{:url("/vehicle/assignVehicleLists")}}?page='+page,
            data:$('#myform').serialize(),
            dataType: 'json',
            beforeSend:function(){
                var index= layer.msg('加载中...',{time:10000});
                layer.style(index, {
                    zIndex:"19991011"  //防止父框的弹出层显示在子框
                });
            },
            success: function(res){
                closeLayerMsg();
                var source=$('#table-lists').html();
                var template = Handlebars.compile(source);
                $('#tbody').html(template(res.msg));
                paginate(res.msg.total,res.msg.page);
                parent.getData();
            },
            error:function(data) {}
        })
    }

    /**
	 * 分配车辆
     */
    function assign(vehicle_id)
	{
         let driver_id = $('#driver_id').val();
        $.ajax({
            type: 'POST',
            url: '{{:url("/driver/saveAssignVehicler")}}',
            data:{driver_id:driver_id,vehicle_id:vehicle_id},
            dataType: 'json',
            beforeSend:function(){
                layer.msg('加载中...',{time:10000});
            },
            success: function(res){
                closeLayerMsg();
                layer.msg(res.msg,{time:1500},function () {
					getData();
                });
            },
            error:function(data) {}
        })
    }


</script>
{include file="common/footer" /}